/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { usePageTabsChange } from '@/utils/hooks/pageTabs';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import { useRef, useEffect, useMemo } from 'react';
import { getAliappParamsList, joinMiniProgramBuildTask } from '@/services/mini/aliapp';

export interface AliappIndexProps {
  type: string;
  activeKey: string;
}
export interface AliappListItemType {
  id: string;
  contactor: string;
  app_name: string;
  phone: string;
  auth_appid: string;
  appid: string;
  create_time: string;
  check_time: string;
  update_time: string;
  refund_time: string;
  inn_account: string;
  merchant: string;
  out_door_pic: string;
  special_license_pic: string[];
  license_info: {
    license_name: string;
    license_number: string;
    license_valid_date: string;
    license_pic: string;
  };
  reject_reason: string;
  mini_category: string;
  user_id: string;
  app_status: string;
}

export function useMiniAliappTable(props: AliappIndexProps) {
  const { activeKey, type } = props;
  const [show] = usePageTabsChange();
  const actionRef = useRef<ActionType>();

  const getList = async (params: { current: any; pageSize: any }) => {
    const { current: page, pageSize: size } = params;
    return await getAliappParamsList({ status: activeKey, page, size });
  };
  const columns: ProColumns<AliappListItemType>[] = useMemo(
    () => [
      {
        title: '小程序名称',
        dataIndex: 'app_name',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: 'appid',
        dataIndex: 'auth_appid',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '手机号',
        dataIndex: 'phone',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '用户id',
        dataIndex: 'user_id',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '姓名',
        dataIndex: 'contactor',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '创建时间',
        dataIndex: 'create_time',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '状态',
        dataIndex: 'app_status',
        search: false,
        align: 'center',
        ellipsis: true,
      },
      // {
      //   title: '提审时间',
      //   dataIndex: 'check_time',
      //   search: false,
      //   align: 'center',
      //   ellipsis: true,
      //   hideInTable: !(activeKey == '2'),
      // },
      // {
      //   title: '上架时间',
      //   dataIndex: 'update_time',
      //   search: false,
      //   align: 'center',
      //   ellipsis: true,
      //   hideInTable: !(activeKey == '3'),
      // },
      // {
      //   title: '驳回时间',
      //   dataIndex: 'refund_time',
      //   search: false,
      //   align: 'center',
      //   ellipsis: true,
      //   hideInTable: !(activeKey == '4'),
      // },
    ],
    [activeKey],
  );

  const reloadTable = () => {
    actionRef.current?.reload();
  };

  useEffect(() => {
    if (show && activeKey && activeKey == type) {
      reloadTable();
    }
  }, [show, activeKey, type]);

  const joinAppBuildTask = async (id: string) => {
    const status = await joinMiniProgramBuildTask({ auth_appid: id });
    if (status) {
      reloadTable();
    }
  };

  return {
    actionRef,
    getList,
    columns,
    reloadTable,
    joinAppBuildTask,
  };
}
